package queue;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Title: ListQueue
 * Author: Michael Satran Jr
 * Date: 10/25/2011
 */

/**
 * This is a Queue that is implemented by the QueueADT.
 * This uses a LinkedList as a backing-store.
 * 
 * @author Michael Satran Jr
 * @version 1
 * @param <T> This is the type of elements this Queue will store.
 */
public class ListQueue<T> implements QueueADT<T>
{
  /**
   * This is the List (Which is a LinkedList) that stores the 
   * data in the Queue (Also known as the backing_store).
   */
  private final List<T> my_backing_store;
  
  /**
   * This creates a new ListQueue.
   */
  public ListQueue()
  {
    my_backing_store = new LinkedList<T>();
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public void enque(final T e) 
  {
    
    ((LinkedList<T>) my_backing_store).addLast(e);
  }

  /**
   * This method assumes that the Queue is not empty at the time it is called.
   * {@inheritDoc}
   */
  @Override
  public T deque() 
  {
    return ((LinkedList<T>) my_backing_store).removeFirst();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public T peek() 
  {
    return ((LinkedList<T>) my_backing_store).getFirst();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int size() 
  {
    return my_backing_store.size();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isEmpty() 
  {
    return my_backing_store.isEmpty();
  }
  
  /**
   * This toString method will return each elements toString() method into one string so it is
   * assumed that T is a type that has toString() method and is readable.
   * {@inheritDoc}
   */
  public String toString()
  {
    final StringBuffer temp = new StringBuffer();
    if (my_backing_store.isEmpty())
    {
      temp.append("Empty Queue");
    } 
    else
    {
      temp.append("front");
    }
    
    final Iterator<T> i = my_backing_store.iterator();
    while (i.hasNext())
    {
      final T next = i.next();
      temp.append("<-[");
      if (next == null)
      {
        temp.append("null");
      }
      else
      {
        temp.append(next.toString());
      }
      temp.append(']');
    }
    
    temp.append("<-end");
    
    return temp.toString();
  }

}